<?php

namespace App\Models;

use Auth;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Content extends Model
{
    use HasFactory;

    protected $guarded = [];

    protected $casts = ['fields' => 'array', 'top' => 'boolean', 'recommended' => 'boolean'];


    /**
     * 操作权限
     * @return bool[]
     * @throws BindingResolutionException
     */
    public  function getPermissionsAttribute()
    {
        return [
            'create' => Auth::check() && Auth::user()->can('create', $this),
            'update' => Auth::check() && Auth::user()->can('update', $this),
            'delete' => Auth::check() && Auth::user()->can('delete', $this),
        ];
    }

    /**
     * 栏目关联
     * @return BelongsTo
     */
    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    /**
     * 按栏目筛选文章内容
     */

    public function scopeFilter($query, $id)
    {
        return $query->where('category_id', $id);
    }
}
